package com.fg.cloud.manager.service.factory.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.fg.cloud.common.finals.Agency;
import com.fg.cloud.common.finals.FacImages;
import com.fg.cloud.common.shiro.UserUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.factory.FactoryImagesEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.factory.FactoryImagesMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.factory.FactoryImagesService;


/**
  * Description:厂商图片Service-impl
  * @author shaoqiping
  * @date 2017年8月3日上午9:42:21
*/
@Service
public class FactoryImagesServiceImpl extends BaseServiceImpl implements FactoryImagesService {
	

	@Autowired
	private FactoryImagesMapper imagesMapper;

	@Override
	public Map<String,Object> selectByFacId(Long facId,int flag) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(flag==FacImages.STATIC){
			List<FactoryImagesEntity> imgs=imagesMapper.selectStaticByFacId(facId);
			List<FactoryImagesEntity> tCode=new ArrayList<FactoryImagesEntity>();
			FactoryImagesEntity imagesEntity=null;
			for (FactoryImagesEntity factoryImagesEntity : imgs) {
				if(factoryImagesEntity.getImgType()==1){
					imagesEntity=factoryImagesEntity;
				}
				if(factoryImagesEntity.getImgType()==3){
					tCode.add(factoryImagesEntity);
				}
			}
			if(imagesEntity==null){
				map.put("logo", null);
			}else{
				map.put("logo", imagesEntity.getPicUrl());
			}
			map.put("tCode", tCode);
		}else if(flag==FacImages.MOVE){
			List<FactoryImagesEntity> ads=imagesMapper.selectMoveByFacId(facId);
			map.put("ads", ads);
		}else{
			List<FactoryImagesEntity> imgs=imagesMapper.selectByFacId(facId);
			List<FactoryImagesEntity> tCode=new ArrayList<FactoryImagesEntity>();
			List<FactoryImagesEntity> ads=new ArrayList<FactoryImagesEntity>();
			FactoryImagesEntity imagesEntity=null;
			for (FactoryImagesEntity factoryImagesEntity : imgs) {
				if(factoryImagesEntity.getImgType()==1){
					imagesEntity=factoryImagesEntity;
				}
				if(factoryImagesEntity.getImgType()==2){
					ads.add(factoryImagesEntity);
				}
				if(factoryImagesEntity.getImgType()==3){
					tCode.add(factoryImagesEntity);
				}
			}
			if(imagesEntity==null){
				map.put("logo", null);
			}else{
				map.put("logo", imagesEntity.getPicUrl());
			}
			map.put("tCode", tCode);
			map.put("ads", ads);
		}
		return map;
	}
	
	@Override
	public Map<String, Object> selectByAgeId(Long ageId,Long facId,int flag) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(flag==FacImages.STATIC){
			List<FactoryImagesEntity> imgs=imagesMapper.selectStaticByFacId(facId);
			List<FactoryImagesEntity> tCode=new ArrayList<FactoryImagesEntity>();
			FactoryImagesEntity imagesEntity=null;
			for (FactoryImagesEntity factoryImagesEntity : imgs) {
				if(factoryImagesEntity.getImgType()==1){
					imagesEntity=factoryImagesEntity;
				}
				if(factoryImagesEntity.getImgType()==3){
					tCode.add(factoryImagesEntity);
				}
			}
			if(imagesEntity==null){
				map.put("logo", null);
			}else{
				map.put("logo", imagesEntity.getPicUrl());
			}
			map.put("tCode", tCode);
		}else if(flag==FacImages.MOVE){
			List<FactoryImagesEntity> ads=new ArrayList<FactoryImagesEntity>();
			AgencyUserVo user=(AgencyUserVo) UserUtils.getCurrentUser();
			if(user.getPlaceorder()<1){
				ads=imagesMapper.selectAds(facId);
			}else{
				ads=imagesMapper.selectByAgeId(ageId, facId, Agency.getTableName(facId));
			}
			map.put("ads", ads);
		}
		return map;
	}
	
	@Override
	@Transactional
	public boolean deleteByPrimaryKeys(Long[] ids) {
		return imagesMapper.deleteByPrimaryKeys(ids) > 0 ? true : false;
	}
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return imagesMapper;
	}
	
	@Override
	@Transactional
	public int insertSelectives(Long facId,String logo,String ads,String tCode) {
		List<FactoryImagesEntity> imgs = new ArrayList<FactoryImagesEntity>();
		if(logo!=null&&!StringUtils.isEmpty(logo)){
			imagesMapper.deleteOlds(facId, 1);
			logo=StringEscapeUtils.unescapeHtml4(logo);
			FactoryImagesEntity entity=new FactoryImagesEntity();
			entity.setFacId(facId);
			entity.setImgType(1);
			entity.setPicUrl(logo);
			entity.setProUrl("");
			entity.setSeq(0);
			imgs.add(entity);
		}
		if(ads!=null&&!StringUtils.isEmpty(ads)){
			imagesMapper.deleteOlds(facId, 2);
			ads=StringEscapeUtils.unescapeHtml4(ads);
			JSONArray json= JSONArray.parseArray(ads);
			List<FactoryImagesEntity> list = json.toJavaList(FactoryImagesEntity.class);
			for (FactoryImagesEntity image : list) {
				image.setFacId(facId);
				image.setImgType(2);
				imgs.add(image);
			}
		}
		if(tCode != null&&!StringUtils.isEmpty(tCode)){
			imagesMapper.deleteOlds(facId, 3);
			tCode=StringEscapeUtils.unescapeHtml4(tCode);
			JSONArray json= JSONArray.parseArray(tCode);
			List<FactoryImagesEntity> list = json.toJavaList(FactoryImagesEntity.class);
			for (FactoryImagesEntity image : list) {
				image.setFacId(facId);
				image.setImgType(3);
				image.setProUrl("");
				imgs.add(image);
			}
		}
		if(imgs.size()==0){
			return 0;
		}
		return imagesMapper.insertSelectives(imgs);
	}

	@Override
	public FactoryImagesEntity selectByPrimaryKey(long id) {
		return imagesMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<FactoryImagesEntity> findAds(Long facId) {
		List<FactoryImagesEntity> ads=new ArrayList<FactoryImagesEntity>();
		AgencyUserVo user=(AgencyUserVo) UserUtils.getCurrentUser();
		if(user.getPlaceorder()<1){
			ads=imagesMapper.selectAds(facId);
		}else{
			ads=imagesMapper.selectByAgeId(user.getAgeId(), facId, Agency.getTableName(facId));
		}
		return ads;
	}
}
